<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>超市管理系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            padding: 30px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .header h1 {
            color: #333;
            margin: 0;
            font-size: 2.5em;
        }
        .login-form {
            max-width: 400px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background: #f9f9f9;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        .btn {
            width: 100%;
            padding: 12px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s;
        }
        .btn:hover {
            background: #0056b3;
        }
        .btn:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .api-test {
            margin-top: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background: #f0f8ff;
        }
        .api-test h3 {
            margin-top: 0;
            color: #333;
        }
        .test-buttons {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        .test-btn {
            padding: 8px 16px;
            background: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .test-btn:hover {
            background: #218838;
        }
        .status {
            margin-top: 20px;
            padding: 10px;
            background: #e9ecef;
            border-radius: 4px;
            font-family: monospace;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🏪 超市管理系统</h1>
            <p>前后端连接测试页面</p>
        </div>

        <!-- 登录表单 -->
        <div class="login-form">
            <h3>用户登录</h3>
            <form id="loginForm">
                <div class="form-group">
                    <label for="username">用户名:</label>
                    <input type="text" id="username" name="username" value="admin" required>
                </div>
                <div class="form-group">
                    <label for="password">密码:</label>
                    <input type="password" id="password" name="password" value="123456" required>
                </div>
                <button type="submit" class="btn" id="loginBtn">登录</button>
            </form>
            <div id="loginResult" class="result"></div>
        </div>

        <!-- API测试区域 -->
        <div class="api-test">
            <h3>API接口测试</h3>
            <div class="test-buttons">
                <button class="test-btn" onclick="testConnection()">测试连接</button>
                <button class="test-btn" onclick="testAuth()">测试认证</button>
                <button class="test-btn" onclick="testProducts()">测试商品API</button>
                <button class="test-btn" onclick="testMembers()">测试会员API</button>
            </div>
            <div id="apiStatus" class="status"></div>
        </div>
    </div>

    <script>
        let authToken = null;

        // 登录功能
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const loginBtn = document.getElementById('loginBtn');
            const resultDiv = document.getElementById('loginResult');
            
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    authToken = data.data.token;
                    resultDiv.className = 'result success';
                    resultDiv.style.display = 'block';
                    resultDiv.innerHTML = `✅ 登录成功！<br>欢迎，${data.data.user.realName || username}`;
                } else {
                    resultDiv.className = 'result error';
                    resultDiv.style.display = 'block';
                    resultDiv.innerHTML = `❌ 登录失败：${data.message}`;
                }
            } catch (error) {
                resultDiv.className = 'result error';
                resultDiv.style.display = 'block';
                resultDiv.innerHTML = `❌ 网络错误：${error.message}`;
            } finally {
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            }
        });

        // 测试连接
        async function testConnection() {
            const statusDiv = document.getElementById('apiStatus');
            statusDiv.innerHTML = '🔄 测试连接中...';
            
            try {
                const response = await fetch('/');
                if (response.status === 401) {
                    statusDiv.innerHTML = '✅ 后端连接正常 (返回401未授权，符合预期)';
                } else {
                    statusDiv.innerHTML = `✅ 后端连接正常 (状态码: ${response.status})`;
                }
            } catch (error) {
                statusDiv.innerHTML = `❌ 连接失败：${error.message}`;
            }
        }

        // 测试认证
        async function testAuth() {
            const statusDiv = document.getElementById('apiStatus');
            statusDiv.innerHTML = '🔄 测试认证中...';
            
            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username: 'admin', password: '123456' })
                });
                
                const data = await response.json();
                if (response.ok) {
                    authToken = data.data.token;
                    statusDiv.innerHTML = '✅ 认证测试成功，已获取Token';
                } else {
                    statusDiv.innerHTML = `❌ 认证失败：${data.message}`;
                }
            } catch (error) {
                statusDiv.innerHTML = `❌ 认证测试失败：${error.message}`;
            }
        }

        // 测试商品API
        async function testProducts() {
            const statusDiv = document.getElementById('apiStatus');
            statusDiv.innerHTML = '🔄 测试商品API中...';
            
            if (!authToken) {
                statusDiv.innerHTML = '❌ 请先登录获取Token';
                return;
            }
            
            try {
                const response = await fetch('/api/mybatis/products', {
                    headers: {
                        'Authorization': `Bearer ${authToken}`,
                        'Content-Type': 'application/json'
                    }
                });
                
                const data = await response.json();
                if (response.ok) {
                    statusDiv.innerHTML = `✅ 商品API测试成功，返回${data.data ? data.data.length : 0}条记录`;
                } else {
                    statusDiv.innerHTML = `❌ 商品API测试失败：${data.message}`;
                }
            } catch (error) {
                statusDiv.innerHTML = `❌ 商品API测试失败：${error.message}`;
            }
        }

        // 测试会员API
        async function testMembers() {
            const statusDiv = document.getElementById('apiStatus');
            statusDiv.innerHTML = '🔄 测试会员API中...';
            
            if (!authToken) {
                statusDiv.innerHTML = '❌ 请先登录获取Token';
                return;
            }
            
            try {
                const response = await fetch('/api/mybatis/members', {
                    headers: {
                        'Authorization': `Bearer ${authToken}`,
                        'Content-Type': 'application/json'
                    }
                });
                
                const data = await response.json();
                if (response.ok) {
                    statusDiv.innerHTML = `✅ 会员API测试成功，返回${data.data ? data.data.length : 0}条记录`;
                } else {
                    statusDiv.innerHTML = `❌ 会员API测试失败：${data.message}`;
                }
            } catch (error) {
                statusDiv.innerHTML = `❌ 会员API测试失败：${error.message}`;
            }
        }

        // 页面加载时自动测试连接
        window.onload = function() {
            testConnection();
        };
    </script>
</body>
</html>